<?php
class mstrstock_model extends CI_Model{
	private $KodeStok;
    private $Kodesupplier;
	private $suppliername;
    private $KodeDetailBarang;
	private $Productname;
  	//private $StockAwal;
	private $StockAkhir;
    private $CreatedBy;
    private $CreatedOn;
    private $LastUpdateBy;
  	private $LastUpdateOn;
  	private $Active;
	
	function setKodeStok($KodeStok){
		$this->KodeStok = $KodeStok;
	}
	function getKodeStok(){
		return $this->KodeStok;
	}
	
	function setKodesupplier($Kodesupplier){
		$this->Kodesupplier = $Kodesupplier;
	}
	function getKodesupplier(){
		return $this->Kodesupplier;
	}
	
	function setsuppliername($suppliername){
		$this->suppliername = $suppliername;
	}
	function getsuppliername(){
		return $this->suppliername;
	}
	
	function setKodeDetailBarang($KodeDetailBarang){
		$this->KodeDetailBarang = $KodeDetailBarang;
	}
	function getKodeDetailBarang(){
		return $this->KodeDetailBarang;
	}
	
	function setProductname($Productname){
		$this->Productname = $Productname;
	}
	function getProductname(){
		return $this->Productname;
	}
	
	/*function setStockAwal($StockAwal){
		$this->StockAwal = $StockAwal;
	}
	function getStockAwal(){
		return $this->StockAwal;
	}*/
	
	function setStockAkhir($StockAkhir){
		$this->StockAkhir = $StockAkhir;
	}
	function getStockAkhir(){
		return $this->StockAkhir;
	}
	
	function setCreatedBy($c_by){
		$this->createdby = $c_by;
	}
	function getCreatedBy(){
		return $this->createdby;
	}

	function setCreatedOn($dateNow){
		//$dateNow = date('Y-m-d H:i:s');
		$this->createdon = $dateNow;
	}
	function getCreatedOn(){
		return $this->createdon;
	}

	function setLastUpdateBy($u_by){
		$this->lastupdateby = $u_by;
	}
	function getLastUpdateBy(){
		return $this->lastupdateby;
	}

	function setLastUpdateOn($dateNow){
		//$dateNow = date('Y-m-d H:i:s');
		$this->lastupdateon = $dateNow;
	}
	function getLastUpdateOn(){
		return $this->lastupdateon;
	}

	function setActive($active_val){
		$this->active = $active_val;
	}

	function getActive(){
		return $this->active;
	}
	
	// Call the module constructor
	function __construct(){
		parent::__construct();
	}
	
	function getstockCode()
	{
		$query = $this->db->query("select MAX(RIGHT(KodeStok,4)) as kdstck from stock");
		$kd = "";
		
		if($query-> num_rows() > 0)
		{
			foreach($query->result() as $idstck)
			{
				$tmpstck = ((int)$idstck->kdstck)+1;
				$kd = sprintf("%04s", $tmpstck);
			}
			
		}
		else
		{
			$kd = "0001";
		}
		
		$kd = "ST".$kd; 
		
		return $kd;
	}
	
	function validasistock()
	{
		
		$query = $this->db->query("select count(KodeDetailBarang) as totalbrg from stock 
		where KodeDetailBarang='". $this->getKodeDetailBarang() ."' and Kodesupplier='". $this->getKodesupplier() ."'");
		$row = $query->row();
		return $row->totalbrg;
	}
	
	function insert()
	{
		$data = array(
			'KodeStok' => $this->getKodeStok(),
			'Kodesupplier' => $this->getKodesupplier(),
			'KodeDetailBarang' => $this->getKodeDetailBarang(),
			//'StockAwal' => $this->getStockAwal(),
			'StockAkhir' => $this->getStockAkhir(),
			'Createdby' => $this->getCreatedBy(),
			'Createdon' => $this->getCreatedOn(),
			'Lastupdateby' => $this->getLastupdateby(),
			'Lastupdateon' => $this->getLastupdateon(),
			'Active' => '1'					
		);
		
		$this->db->insert('stock',$data);
	}
	
	function ubahmstrstock() 
	{
            $data = array(
                'Kodesupplier' => $this->getKodesupplier(),
                'KodeDetailBarang' => $this->getKodeDetailBarang(),
                //'StockAwal' => $this->getStockAwal(),
				'StockAkhir' => $this->getStockAkhir(),
                'Lastupdateby' => $this->getLastupdateby(),
				'Lastupdateon' => $this->getLastupdateon(),
            );
 
        $this->db->where(array('KodeStok' => $this->getKodeStok()));
        $this->db->update('stock', $data);
    }

    function deletemstrstock() {
        $data = array(
            'active' => '0'
        );
        $this->db->where(array('KodeStok' => $this->getKodeStok()));
        $this->db->update('stock', $data);
    }
	
	function getList(){
		$this->db->select("stock.KodeStok,supplier.Kodesupplier,supplier.suppliername,detail_product.KodeDetailBarang,detail_product.Productname,stock.StockAkhir")
		->join('supplier', 'supplier.Kodesupplier=stock.Kodesupplier')
		->join('detail_product', 'detail_product.KodeDetailBarang=stock.KodeDetailBarang')
		->where(array('stock.Active'=>'1'));
		$this->db->order_by('KodeStok', 'asc');
		$query = $this->db->get('stock');
		return $query->result();
	}
	
	function getListlimit($limit,$start){
		$this->db->select("stock.KodeStok,supplier.Kodesupplier,supplier.suppliername,detail_product.KodeDetailBarang,detail_product.Productname,stock.StockAkhir")
		->join('supplier', 'supplier.Kodesupplier=stock.Kodesupplier')
		->join('detail_product', 'detail_product.KodeDetailBarang=stock.KodeDetailBarang')
		->where(array('stock.Active'=>'1'))
		->limit($limit,$start);
		$this->db->order_by('KodeStok', 'asc');
		$query = $this->db->get('stock');
		return $query->result();
	}
	
	function countGetList(){
		$this->db->select("stock.KodeStok,supplier.Kodesupplier,supplier.suppliername,detail_product.KodeDetailBarang,detail_product.Productname,stock.StockAkhir")
		->join('supplier', 'supplier.Kodesupplier=stock.Kodesupplier')
		->join('detail_product', 'detail_product.KodeDetailBarang=stock.KodeDetailBarang')
		->where(array('stock.Active'=>'1'));
		$this->db->order_by('KodeStok', 'asc');
		$query = $this->db->get('stock');
		return $query->num_rows();
	}
	
	function getListkdbrg(){
		$this->db->select()->where(array('Active'=>'1'));
		$query = $this->db->get('detail_product');
		return $query->result();
	}
	
	function getkodestock()
	{
		$this->db->select("stock.KodeStok,supplier.Kodesupplier,supplier.suppliername,detail_product.KodeDetailBarang,detail_product.Productname,stock.StockAkhir")
		->join('supplier', 'supplier.Kodesupplier=stock.Kodesupplier')
		->join('detail_product', 'detail_product.KodeDetailBarang=stock.KodeDetailBarang')
		->where(array('stock.KodeStok' => $this->getKodeStok(),'stock.active'=>'1'));
		$query = $this->db->get('stock');
		return $query->result();
	}
	
	function getkdstocklike(){
		$this->db->select("stock.KodeStok,supplier.Kodesupplier,supplier.suppliername,detail_product.KodeDetailBarang,detail_product.Productname,stock.StockAkhir")
		->join('supplier', 'supplier.Kodesupplier=stock.Kodesupplier')
		->join('detail_product', 'detail_product.KodeDetailBarang=stock.KodeDetailBarang')
		->where(array('stock.Active'=>'1'))->like(array('stock.KodeStok' => $this->getKodeStok()));
		$query = $this->db->get('stock');
		return $query->result();
	}
	
	function getkdbaranglike(){
		$this->db->select("stock.KodeStok,supplier.Kodesupplier,supplier.suppliername,detail_product.KodeDetailBarang,detail_product.Productname,stock.StockAkhir")
		->join('supplier', 'supplier.Kodesupplier=stock.Kodesupplier')
		->join('detail_product', 'detail_product.KodeDetailBarang=stock.KodeDetailBarang')
		->where(array('stock.Active'=>'1'))->like(array('detail_product.Productname' => $this->getProductname()));
		$query = $this->db->get('stock');
		return $query->result();
	}
	
	function getsupplierlike(){
		$this->db->select("stock.KodeStok,supplier.Kodesupplier,supplier.suppliername,detail_product.KodeDetailBarang,detail_product.Productname,stock.StockAkhir")
		->join('supplier', 'supplier.Kodesupplier=stock.Kodesupplier')
		->join('detail_product', 'detail_product.KodeDetailBarang=stock.KodeDetailBarang')
		->where(array('stock.Active'=>'1'))->like(array('supplier.suppliername' => $this->getsuppliername()));
		$query = $this->db->get('stock');
		return $query->result();
	}
}